package com.hand.test.controller;/**
 * @author Estelle@앙수청
 * @date 2019/7/20 11:06
 */

import com.hand.test.domain.Matter;
import com.hand.test.domain.Stockin;
import com.hand.test.domain.Stockout;
import com.hand.test.service.StockInAndOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  @author Estelle@앙수청
 *  @date 2019/7/20 11:06
 */
@RestController

public class StockInAndOutController {

    @Autowired
    private StockInAndOutService stockInAndOutService ;

    @PostMapping("/stockin")
    public void stockInput(@RequestBody Stockin stockin){
        Integer matterId = stockin.getMatterId();
        Matter matter = stockInAndOutService.queryMatterExistByStore(matterId);
        if (matter!=null){
            // 增加入库信息
            Integer add = stockInAndOutService.addStockin(stockin);
            // 修改库存数量
            stockInAndOutService.increMatterStockCount(stockin);
        }else {
            // TODO
            System.out.println("库存无此物料，请先添加此物料信息");
        }
    }

    @PutMapping("/stockout")
    public void stockOutput(@RequestBody Stockout stockout){
        // 先查询出库的物料id
        Integer matterId = stockout.getMatterId();
        Matter matter = stockInAndOutService.queryMatterExistByStore(matterId);
        if (matter!=null){
            // 增加出库信息
            stockInAndOutService.addStockout(stockout);
            // 修改库存数量
            stockInAndOutService.decreMatterStockCount(stockout);
        }else {
            // TODO
            System.out.println("库存无此物料，出库失败");
        }
    }

    @GetMapping("/all/stockin")
    public List<Stockin> queryAllStockin(){
        return stockInAndOutService.queryAllStockin();
    }

    @GetMapping("/all/stockout")
    public List<Stockin> queryAllStockout(){
        return stockInAndOutService.queryAllStockout();
    }
}
